﻿using AutoMapper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using QMERP.Domain.Dtos;
using QMERP.Domain.Enums;
using QMERP.Infrastructure.CustomException;
using QMERP.Infrastructure.EFCore;
using QMERP.Infrastructure.Extensions;
using QMERP.Infrastructure.Tools;
using System.Data;

namespace QMERP.Application.Services
{
    public class BasicService : IBasicService
    {
        private readonly IMapper _mapper;
        private readonly QMERPDbContext _db;
        private readonly IWebHostEnvironment _webHostEnvironment;
        public BasicService(IMapper mapper, QMERPDbContext db, IWebHostEnvironment webHostEnvironment)
        { 
            _mapper = mapper;
            _db = db;
            _webHostEnvironment = webHostEnvironment;
        }
        /// <summary>
        /// Excel转DataTable
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public DataTable GetDataTable(IFormFile? file)
        {
            if (file == null)
                throw new ApiException("文件不能为空!");
            Stream stream = file.OpenReadStream();

            return ExcelHelper.ExcelStreamToDataTable(stream);
        }
        /// <summary>
        /// XYExcel转DataTable
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public DataTable GetXYTable(IFormFile? file)
        {
            if (file == null)
                throw new ApiException("文件不能为空!");
            Stream stream = file.OpenReadStream();

            return ExcelHelper.XYExcelStreamToDataTable(stream);
        }
        /// <summary>
        /// XYExcel转LensDetails
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public List<LensDetailResponse> GetLensDetails(IFormFile? file)
        {
            if (file == null)
                throw new ApiException("文件不能为空!");
            Stream stream = file.OpenReadStream();
            return stream.ToLensDetails();
        }
        /// <summary>
        /// XYExcel转DataTable
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public async Task<XYTableViewResponse> GetXYTableView(XYTableViewRequest request)
        {
            //List<LensDetail> lensDetails = new List<LensDetail>();
            string title = "";
            if (request.Type == (int)XYTableTypeEnum.Stock)
            {
                //var stock = await _db.Stock.Include(x => x.StockDetails).Include(x => x.Warehouse).Include(x => x.Lens).Where(x => x.WarehouseId == request.WarehouseId && x.LensId == request.LensId).FirstOrDefaultAsync();
                //if (stock == null)
                //    throw new ApiException("镜种库存数据未找到!");
                //lensDetails = _mapper.Map<List<LensDetail>>(stock.StockDetails);
                //title = "库存数据";
            }
            else if (request.Type == (int)XYTableTypeEnum.BasicStock)
            {
                //var list = await _db.StockDetail.Include(x => x.Stock).Include(x => x.Stock!.Warehouse).Include(x => x.Stock!.Lens).Where(x => x.Stock!.WarehouseId == request.WarehouseId && x.Stock!.LensId == request.LensId).ToListAsync();
                //list.ForEach(x => x.Qty = x.BasicQty);
                //lensDetails = _mapper.Map<List<LensDetail>>(list);
                //title = "基本库存数据";
            }
            else if (request.Type == (int)XYTableTypeEnum.ActionLog)
            {
                //var list = await _db.StockActionLogDetail.Include(x => x.StockActionLog).Include(x => x.StockActionLog!.Warehouse).Include(x => x.StockActionLog!.Lens).Where(x => x.StockActionLogId == request.Id!.Value).ToListAsync();
                //lensDetails = _mapper.Map<List<LensDetail>>(list);
                //title = "出入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_Opening)
            {
                //var list = await _db.StockLogDetail.Include(x => x.StockLog).Include(x => x.StockLog!.Warehouse).Include(x => x.StockLog!.Lens).Where(x => x.StockLog!.InventoryDate == request.InventoryDateBegin && x.StockLog.WarehouseId == request.WarehouseId && x.StockLog.LensId == request.LensId).ToListAsync();
                //lensDetails = _mapper.Map<List<LensDetail>>(list);
                //title = "期初库存记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_Closing)
            {
                //var list = await _db.StockLogDetail.Include(x => x.StockLog).Include(x => x.StockLog!.Warehouse).Include(x => x.StockLog!.Lens).Where(x => x.StockLog!.InventoryDate == request.InventoryDateEnd && x.StockLog.WarehouseId == request.WarehouseId && x.StockLog.LensId == request.LensId).ToListAsync();
                //lensDetails = _mapper.Map<List<LensDetail>>(list);
                //title = "期末库存记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_PurchaseIn)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishInLog(ActionLogReasonInEnum.PurchaseIn, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "采购入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_OrderIn)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishInLog(ActionLogReasonInEnum.OrderIn, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "订单入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_GoodsReturn)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishInLog(ActionLogReasonInEnum.GoodsReturn, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "退货入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_InventoryIn)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishInLog(ActionLogReasonInEnum.InventoryIn, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "盘点入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_ReworkIn)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishInLog(ActionLogReasonInEnum.ReworkIn, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "返工入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_TransferIn)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishInLog(ActionLogReasonInEnum.TransferIn, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "转仓入库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_SaleOut)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishOutLog(ActionLogReasonOutEnum.SaleOut, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "销售出库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_OrderOut)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishOutLog(ActionLogReasonOutEnum.OrderOut, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "订单出库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_BadOut)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishOutLog(ActionLogReasonOutEnum.BadOut, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "不良出库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_InventoryOut)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishOutLog(ActionLogReasonOutEnum.InventoryOut, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "盘点出库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_ReworkOut)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishOutLog(ActionLogReasonOutEnum.ReworkOut, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "返工出库记录";
            }
            else if (request.Type == (int)XYTableTypeEnum.InOutSummary_TransferOut)
            {
                //lensDetails = await _db.ActionLogDetail.GetFinnishOutLog(ActionLogReasonOutEnum.TransferOut, request.WarehouseId!.Value, request.LensId!.Value, request.InventoryDateBegin!.Value, request.InventoryDateEnd!.Value).ToListAsync();
                //title = "转仓出库记录";
            }
            //if (lensDetails == null || lensDetails.Count <= 0)
            //{
            //    var stock = await _db.Stock.Include(x => x.StockDetails).Include(x => x.Warehouse).Include(x => x.Lens).Where(x => x.WarehouseId == request.WarehouseId && x.LensId == request.LensId).FirstOrDefaultAsync();
            //    if (stock == null)
            //        throw new ApiException("镜种库存数据未找到!");
            //    lensDetails = _mapper.Map<List<LensDetail>>(stock.StockDetails);
            //    lensDetails.ForEach(x => x.Qty = 0);
            //}
            XYTableViewResponse response = new()
            {
                //XYTable = ExcelHelper.LensDetailsToXYTable(lensDetails),
                //WarehouseId = lensDetails[0].WarehouseId,
                //WarehouseCode = lensDetails[0].WarehouseCode,
                //LensId = lensDetails[0].LensId,
                //LensCode = lensDetails[0].LensCode
            };
            response.Title = $"{response.WarehouseCode}_{response.LensCode}{title}";


            return response;
        }
        /// <summary>
        /// 导出XYTableExcel
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public MemoryStream DownLoadXYTableExcel(DownLoadXYTableExcelRequest request)
        {
            //List<LensResponse> lensDetails = ExcelHelper.XYTableToLensDetails(request.XYTable, request.WarehouseId, request.WarehouseCode, request.LensId, request.LensCode);
            //return ExcelHelper.LensDetailsToXYExcelStream(lensDetails);
            return null;
        }
        /// <summary>
        /// 获取所有图标名称
        /// </summary>
        /// <returns></returns>
        public List<string> GetIcons()
        {
            string webRootPath = _webHostEnvironment.WebRootPath;
            DirectoryInfo directory = new DirectoryInfo(Path.Combine(webRootPath, "icons"));
            return directory.GetFiles().Select(s => s.Name).ToList();
        }
    }
}
